package testcase.app;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

import api.app.CloseSubject;
import api.app.StartChat;
import api.doctor.UpdateStatus;
import common.Config;
import common.Host;
import common.TestData;
/**
 * 帮忙医APP关闭会话接口CloseSubject测试用例
 * @author gaowen
 *
 */
public class TestCloseSubject
{
	
	/**
	 * 帮忙医APP关闭会话接口CloseSubject测试环境数据驱动
	 * @return 测试数据
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 */
	@DataProvider(name="Test")
	public static String[][] testCloseSubjectDataProvider() throws ParserConfigurationException, SAXException, IOException
	{
		Host.setup("Test");
		TestData tData=new TestData("app/TestCloseSubject.xml");
		return tData.getParam();
	}
	
	/**
	 * 测试前上线医生
	 * @throws IOException
	 */
	@BeforeClass(groups="Test")
	public void testStartChatBeforeTest() throws IOException
	{
		Host.setup("Test");
		String doctorID=Config.getValue("Doctor-Test", "ID");
		String doctorToken=Config.getValue("Doctor-Test", "token");
		UpdateStatus.put("true", "1", doctorID, doctorToken);
	}
	
	/**
	 * 帮忙医APP关闭会话接口CloseSubject测试环境测试用例
	 * @param caseName 用例名
	 * @param subjectId 会话ID
	 * @param token 调接口需要的token
	 * @throws IOException 
	 * @throws JSONException 
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 */
	@Test(dataProvider="Test",groups="Test")
	public void testCloseSubjectTestCase(String caseName,String subjectId,String token) throws IOException, JSONException, ParserConfigurationException, SAXException
	{
		if (subjectId.equals("get"))
		{
			TestData tData=new TestData("app/TestStartChat.xml");
			String scr=StartChat.post(Integer.parseInt(tData.getData("Success", "userID")), tData.getData("Success", "userName"), tData.getData("Success", "userSex"),Integer.parseInt(tData.getData("Success", "age")), tData.getData("Success", "studyID"), tData.getData("Success", "token"));
			JSONObject jsonScr=new JSONObject(scr);
			subjectId=jsonScr.getJSONObject("Data").getString("SubjectID");
		}
		//调用接口
		String result=CloseSubject.get(subjectId, token);
		
		JSONObject jsonResult;
		if (result==null||result.isEmpty())
		{
			jsonResult=null;
		}
		else
		{
			jsonResult=new JSONObject(result);
		}
		
		switch (caseName)
		{
			case "Success":
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "SubjectIdWrong":
				Assert.assertTrue(jsonResult.getBoolean("Data"));
				break;
			case "SubjectIdNull":
				Assert.assertNull(result);
				break;
			case "TokenWrong":
				CloseSubject.get(subjectId, "PKZIbEe+K+DDEVP0VXBLzg==");
				Assert.assertNull(result);
				break;
			case "TokenNull":
				CloseSubject.get(subjectId, "PKZIbEe+K+DDEVP0VXBLzg==");
				Assert.assertNull(result);
				break;
			default:
				break;
		}
	}
	
	/**
	 * 测试后下线医生
	 * @throws IOException
	 */
	@AfterTest(groups="Test")
	public void testStartChatAfterTest() throws IOException
	{
		Host.setup("Test");
		String doctorID=Config.getValue("Doctor-Test", "ID");
		String doctorToken=Config.getValue("Doctor-Test", "token");
		UpdateStatus.put("false", "1", doctorID, doctorToken);
	}
}
